﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace NuMvvm.Services
{
    public class NavigationService : INavigationService
    {
        private System.Windows.Navigation.NavigationService _navigationService;

        public NavigationService()
        {
            var frame = FrameWindow.RootFrame;
            _navigationService = frame.NavigationService;
            _navigationService.Navigated += _navigationService_Navigated;
        }

        private void _navigationService_Navigated(object sender, NavigationEventArgs e)
        {
            CurrentView = (IView)e.Content;
        }

        public IView CurrentView
        {
            get
            {
                return Page.Current;
            }

            private set
            {
                Page.Current = value;
            }
        }

        public void Navigate(object view)
        {
            CurrentView = (IView)Activator.CreateInstance((Type)view);
            _navigationService.Navigate(CurrentView);
        }

        public void Navigate(object view, object arg)
        {
            CurrentView = (IView)Activator.CreateInstance((Type)view, new object[] { arg });
            _navigationService.Navigate(CurrentView);
        }

        public bool CanGoBack
        {
            get
            {
                return _navigationService.CanGoBack;
            }
        }

        public bool CanGoForward
        {
            get
            {
                return _navigationService.CanGoForward;
            }
        }

        public void GoBack()
        {
            _navigationService.GoBack();
        }

        public void GoForward()
        {
            _navigationService.GoForward();
        }
    }
}
